using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace FeedFly
{
    /// <summary>
    /// Feed subclass for RSS feeds
    /// </summary>
    public class FeedRSS : Feed
    {
        public FeedRSS(int folderId, string address, short itemLimit, XmlReader feedReader)
            : base(folderId, address, itemLimit, feedReader)
        {
            // set feed format
            this.format = FeedFormat.RSS;
        }

        public FeedRSS(FeedInterface feed)
            : base(feed)
        { }

        protected override XmlReader AdvanceReaderToItems(XmlReader feedReader)
        {
            feedReader.ReadToFollowing("item");
            return feedReader;
        }

        protected override void Parse(XmlReader feedReader)
        {
            while (feedReader.Read())
            {
                if (feedReader.NodeType == XmlNodeType.Element && feedReader.Depth < 3)
                {
                    switch (feedReader.Name)
                    {
                        case "channel":
                            break;                        
                        case "title":                            
                            this.title = feedReader.ReadString();
                            break;
                        case "link":
                            this.link = feedReader.ReadString();
                            break;
                        case "item":
                            this.AddItem(this.ParseItem(feedReader));
                            break;
                        default:
                            feedReader.ReadString();
                            break;
                    }
                }
            }

            feedReader.Close();
        }

        protected override FeedItem ParseItem(XmlReader feedReader)
        {
            FeedItem item = new FeedItem(this);

            while (feedReader.Read())
            {
                if (feedReader.NodeType == XmlNodeType.Element)
                {
                    switch (feedReader.Name)
                    {
                        case "title":
                        case "dc:title":
                            item.Title = feedReader.ReadString();
                            break;
                        case "guid":
                            item.Guid = feedReader.ReadString();
                            break;
                        case "link":
                            item.Link = feedReader.ReadString();
                            break;
                        case "pubDate":
                        case "dc:date":
                            item.PubDate = Feed.ParseDate(feedReader.ReadString());
                            break;
                        case "author":
                        case "dc:creator":
                            item.Author = feedReader.ReadString();
                            break;
                        case "description":
                            item.Description = feedReader.ReadString();
                            break;
                        default:
                            feedReader.ReadString();
                            break;
                    }
                }
                else if (feedReader.NodeType == XmlNodeType.EndElement && feedReader.Name == "item")
                {
                    if (item.PubDate == DateTime.MinValue)
                        item.PubDate = DateTime.Now;
                    break;
                }
            }

            return item;
        }
    }
}
